package ro.msg.office.beans;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import ro.msg.office.ticketmanager.DO.TicketDO;
import ro.msg.office.ticketmanager.DO.UserDO;
import ro.msg.office.ticketmanager.back.ifc.TicketManager;

/**
 * This class is handling the properties from the detailsAdmin.xhtml file 
 * Here are the methods used to display the information about the selected ticket
 * @author Denisa Sulea
 *
 */
public class DetailsAdminBean {
    @EJB
    TicketManager ticketEJB;
    
    private static TicketDO ticket = new TicketDO();
    private String interval = "";
    private String hasResponse ="";
    private String hasNoResponse= "";
    private String statusS= "";
    private String hasNoResponseB= "";
    
	/**
	 * Method used to set the interval for the current ticket 
	 * @param interval - the new value for the interval
	 */
	public void setInterval(String interval) {
		this.interval = interval;
	}
	/**
	 * Method used to convert the format of the interval to a String format of type (startTime - endTime)
	 * @return - the String format of the interval value
	 */
	public String getInterval() {
		if(LoginBean.getSigned())
		{
		    DateFormat df = new SimpleDateFormat("HH:mm");  
		    String intervalAux = df.format(ticket.getStartTime())+" - "+df.format(ticket.getEndTime()); 
		    if(intervalAux!=null)
				interval = intervalAux;
		}
		return interval;
	}
	
	/**
	 * Method used to update the ticket and send a response for a certain ticket
	 * @return - a String that represents a navigation between pages : goes to the table with tickets if there is
	 * no error, if there is an error, a message will be displayed on the current page
	 */
	public String sendResponse(){
		Long status = new Long(-1);
		if(statusS==null){
			FacesContext facesContext = FacesContext.getCurrentInstance();
	 		FacesMessage fm = new FacesMessage("Please select a status!");
	 		facesContext.addMessage(null, fm);
		    return "errorDetailsAdmin";
		}
		else{
			if(statusS.equals("draft"))
				status = new Long(0);
			else
				if(statusS.equals("pending"))
					status = new Long(1);
				else
					if(statusS.equals("accepted"))
						status = new Long(2);
					else
						if(statusS.equals("rejected"))
							status = new Long(3);
						else
							if(statusS.equals("more details"))
								status = new Long(4);
			ticket.setStatus(status);
			ticket.setStatusString();
			ticket.setApprovalDate(new Date());
			ticketEJB.updateTicketByAdmin(ticket);
			}
		return "backDetailsAdmin";
	}

	/**
	 * Method used to set the selected ticket
	 * @param tickett - the ticket that was selected in the dataTable
	 */
	public static void setTicket(TicketDO tickett) {
		ticket = tickett;
	}
	/**
	 * Method used to get the current ticket that was selected from the dataTable
	 * @return - the value of the current ticket
	 */
	public TicketDO getTicket() {
		return ticket;
	}
	/**
	 *  Method used to set the value of the hasResponse
	 *  (used in xhtml as a property for some components to set them visible or hidden)
	 * @param hasResponse - the new value of visibility
	 */
	public void setHasResponse(String hasResponse) {
		this.hasResponse = hasResponse;
	}
	/**
	 * Method used to get the visibility property from some components in the xhtml file
	 * @return - the value of visibility 
	 * (if the ticket already has a response and this response is pending, 
	 * the administrator can edit the status and message fields, else those fields are hidden)
	 */
	public String getHasResponse() {
		Long status = ticket.getStatus();
		if(LoginBean.getSigned())
		{
			if(status.equals(new Long(1)))//pending
				hasResponse = "visibility:hidden";
			else 
				hasResponse = "visibility:visible";
		}
		return hasResponse;
	}
	/**
	 * Method used to set the value of the hasNoResponse
	 * (used in xhtml as a property for some components to set them visible or hidden)
	 * @param hasNoResponse - the new value of visibility
	 */
	public void setHasNoResponse(String hasNoResponse) {
		this.hasNoResponse = hasNoResponse;
	}
	/**
	 * Method used to get the visibility property from some components in the xhtml file
	 * @return - the value of visibility 
	 * (if the ticket already has a response and this response is pending, 
	 * the administrator can edit the status and message fields, else those fields are hidden)
	 */
	public String getHasNoResponse() {
		Long status = ticket.getStatus();
		if(LoginBean.getSigned())
		{
			if(status.equals(new Long(1)))
				hasNoResponse = "visibility:visible";
			else 
				hasNoResponse = "visibility:hidden";
		}
		return hasNoResponse;
	}
	/**
	 * Method used to set the value of the Status as a String
	 * @param statusS - the new status as a String
	 */
	public void setStatusS(String statusS) {
		if(getHasNoResponseB().equals("true")){
			this.statusS = statusS;
		}
	}
	/**
	 * Method used to get the status as a string
	 * @return - the value of the status
	 */
	public String getStatusS() {
		return statusS;
	}
	/**
	 * Method used to set the value of the hasNoResponseB
	 * (used in xhtml as a property for some components to set them required or not required)
	 * @param hasNoResponseB - the new value for the required property in xhtml
	 */
	public void setHasNoResponseB(String hasNoResponseB) {
		this.hasNoResponseB = hasNoResponseB;
	}
	/**
	 * Method used to get the required property from some components in the xhtml file
	 * @return - the value of required property for some components
	 * (if the ticket already has a response and this response is pending, 
	 * the administrator can edit the status and message fields and those fields are required,
	 * else those fields are not required)
	 */
	public String getHasNoResponseB() {
		Long status = ticket.getStatus();
		if(LoginBean.getSigned())
		{
			if(status.equals(new Long(1)))
				hasNoResponseB = "true";
			else 
				hasNoResponseB = "false";
		}
		return hasNoResponseB;
	}
}
